import channelRegiseterApi from '../services/channelRegiseterApi';

export default {
  namespace: 'channelDictionaryModel',
  state: {
    channelsTypesData: [],
    CHANNEL_VALUE: []
  },
  effects: {
    // 渠道类型查询
    *channelsTypes({ payload = {} }, { call, put }) {
      const res = yield call(channelRegiseterApi.channelsTypes, payload);
      const { datas = [] } = res;
      yield put({ type: 'put/channelsTypes', payload: datas });
      yield put({
        type: 'put/channelValue',
        payload: datas.map(item => ({
          text: item.des,
          value: item.value
        }))
      });
    },
    // 渠道启用停用
    *updateStatus({ payload, callback }, { call }) {
      yield call(channelRegiseterApi.channelsupdateStatus, payload);
      callback();
    },
    // 钉钉群查询
    *dingcompanygrouplist({ payload, callback }, { call }) {
      const res = yield call(channelRegiseterApi.dingcompanygrouplist, payload);
      const { success, datas } = res;
      if (success) {
        callback(datas);
      }
    }
  },
  reducers: {
    'put/channelsTypes': (state, { payload }) => ({
      ...state,
      channelsTypesData: payload
    }),
    'put/channelValue': (state, { payload }) => ({
      ...state,
      CHANNEL_VALUE: payload
    })
  }
};
